import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  GlobalKey _key = GlobalKey<FormState>();
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    //根节点，只有一个
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
          appBar: AppBar(
            title: Text("From Demo"),
          ),
          body: InputDemo(),
        ));
  }
}

class InputDemo extends StatefulWidget {
  InputDemo({Key? key}) : super(key: key);

  @override
  _InputDemoState createState() => _InputDemoState();
}

class _InputDemoState extends State<InputDemo> {
  GlobalKey _key = GlobalKey<FormState>();
  TextEditingController _userController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  TextEditingController _emailController = TextEditingController();
  FocusNode _passwordFocusNode = FocusNode();
  FocusNode _emailFocusNode = FocusNode();
  FocusScopeNode? _focusScopeNode;
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    _userController.dispose();
    _passwordController.dispose();
    _emailController.dispose();
    _passwordFocusNode.dispose();
    _emailFocusNode.dispose();
    if (_focusScopeNode != null) {
      _focusScopeNode!.dispose();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Form(
        key: _key,
        child: Column(
          children: [
            TextFormField(
              onFieldSubmitted: (v) {},
              textInputAction: TextInputAction.next,
              autofocus: true,
              controller: _userController,
              validator: (v) {
                if (v == null || v.isEmpty) {
                  return "请输入用户！";
                }
              },
              decoration: InputDecoration(
                  icon: Icon(Icons.account_circle),
                  labelText: "user",
                  hintText: 'your account'),
            ),
            SizedBox(
              height: 8,
            ),
            TextFormField(
              focusNode: _passwordFocusNode,
              controller: _passwordController,
              textInputAction: TextInputAction.next,
              decoration: InputDecoration(
                icon: Icon(Icons.lock),
                labelText: "password",
                hintText: 'your parssword',
              ),
              validator: (v) {
                if (v == null || v.isEmpty) {
                  return "请输入密码！";
                }
              },
              obscureText: true,
            ),
            SizedBox(
              height: 8,
            ),
            TextFormField(
              focusNode: _emailFocusNode,
              controller: _emailController,
              decoration: InputDecoration(
                  icon: Icon(Icons.mail),
                  labelText: "email",
                  hintText: 'your email'),
              validator: (v) {
                if (v == null || v.isEmpty) {
                  return "请输入邮箱！";
                }
              },
              textInputAction: TextInputAction.send,
            ),
            SizedBox(
              height: 16,
            ),
            ElevatedButton(
              onPressed: () {
                if ((_key.currentState as FormState).validate()) {
                  print("true");
                } else {
                  if (_focusScopeNode == null) {
                    _focusScopeNode = FocusScope.of(context);
                  }
                  _focusScopeNode!.requestFocus(_emailFocusNode);
                  print("false");
                }
              },
              child: Text("提交"),
            )
          ],
        ));
  }
}

